package com.struts2.example.action;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.struts2.example.model.Connect;
import com.struts2.example.model.Question;

public class InsertQuestionAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String questionTextBean;
	//
	private Question questionBean;
	private List<Question> listQuestions;

	public String execute() {
		
		listQuestions=new ArrayList<Question>();
		String ret = ERROR;
		Connect conn = new Connect();
		Connection connected = conn.GetConnect();

		try {

			String sql = "insert into  testonline_bang_dai.question values (default,?)";
			PreparedStatement ps = connected.prepareStatement(sql);
			ps.setString(1,questionTextBean);
		    ps.executeUpdate();
		    
		    //////////////////
			String sql1 = "SELECT * FROM testonline_bang_dai.question";
			ps= connected.prepareStatement(sql1);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				questionBean=new Question();
				questionBean.setQuestionId(rs.getInt(1));
				questionBean.setQuestionText(rs.getString(2));
				listQuestions.add(questionBean);
				ret = SUCCESS;
			}
			
		} catch (Exception e) {
			ret = ERROR;
		} finally {
			if (connected != null) {
				try {
					connected.close();
				} catch (Exception e) {
				}
			}
		}
		return ret;
	}

	public String getQuestionTextBean() {
		return this.questionTextBean;
	}

	public void setQuestionTextBean(String questionTextBean) {
		this.questionTextBean = questionTextBean;
	}
	
	//////////

	public Question getQuestionBean() {
		return this.questionBean;
	}

	public void setQuestionBean(Question quesionBean) {
		this.questionBean = quesionBean;
	}
	
	public List<Question> getListQuestions()
	{
		return listQuestions;
	}
	
	public void setListQuestion(List<Question> listQuestions)
	{
		this.listQuestions=listQuestions;
	}
}
